#include "player.h"

static unsigned int imageNumber = -1;
static unsigned int next;
Player::Player(std::string image, float left, float top)
{
	this->imageName = image;
	this->image.LoadFromFile( image );
	this->image.CreateMaskFromColor( sf::Color( 255, 255, 255 ) );
	this->sprite.SetImage( this->image );
	this->setPosition( left, top );
	rotateImage( 0 );
}

Player::Player(const Player &player)
{
	this->imageName = player.imageName;
	this->image.LoadFromFile( player.imageName );
	this->rect.Bottom = player.rect.Bottom;
	this->rect.Top = player.rect.Top;
	this->rect.Left = player.rect.Left;
	this->rect.Right = player.rect.Right;
	this->sprite.SetImage( player.image );
}

Player::~Player()
{
}

void Player::setPosition( float left, float top )
{
	this->sprite.SetPosition( left, top );
	this->rect.Left = left + PLAYER_COLLISION_OFFSET;
	this->rect.Top = top + this->sprite.GetImage()->GetHeight() - TILE_SIZE + (TILE_SIZE / 2 );
	this->rect.Bottom = top + this->sprite.GetImage()->GetHeight();
	this->rect.Right = left + this->sprite.GetImage()->GetWidth() - PLAYER_COLLISION_OFFSET;
}

sf::Rect<float> Player::getRect()
{
	return this->rect;
}

sf::Sprite Player::getSprite()
{
	return this->sprite;
}

void Player::rotateImage( unsigned int direction )
{
	next++;
	if( next == HERO_FRAME_RATE )
	{
		next = 0;
		imageNumber++;
		if( imageNumber > NUM_HERO_FRAMES - 1 )
		{
			imageNumber = 0;
		}
	}
	this->sprite.SetSubRect( sf::IntRect( imageNumber * PLAYER_WIDTH, direction * PLAYER_HEIGHT, ( imageNumber * PLAYER_WIDTH ) + PLAYER_WIDTH, ( direction * PLAYER_HEIGHT ) + PLAYER_HEIGHT  ) );
}

unsigned int rotateImage()
{	
	return imageNumber;
}

void Player::setRect( float top, float bottom, float left, float right )
{
	this->rect.Top = top;
	this->rect.Bottom = bottom;
	this->rect.Left = left;
	this->rect.Right = right;
}

void Player::setInput( sf::Key::Code upKey, sf::Key::Code downKey, sf::Key::Code leftKey, sf::Key::Code rightKey )
{
	this->upInput = upKey;
	this->downInput = downKey;
	this->leftInput = leftKey;
	this->rightInput = rightKey;
}

void Player::input( const sf::Input &input )
{
}